// pages/order/order.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        token: wx.getStorageSync('token'),
        parkLog: undefined,
        state: 1,           //状态
        orderList: [],      //订单列表数据，接口获取
        currentPage0: 1,    //当前页
        currentPage1: 1,
        currentPage2: 1,
        orderList0: [],
        orderList1: [],
        orderList2: [],
        showModel: false,
        existencePassword: false,
        currentParkId: undefined,
        password: '',       //密码
        confirm_password: '',
        isNoMoreData: false,
        winHeight: 900,
        currentTab: 0,      //当前显示tab的下标
        isRefresherTriggered: true,
        navTab: ['未付款', '已付款', '全部'],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function () {

    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        const that = this
        let token = that.data.token
        if (!token) {
            token = wx.getStorageSync('token')
            if (token) {
                that.setData({token: token})
            }
        }
        this.fresherRefresh()
    },
    /**
     * 获取停车数据
     */
    getData(current) {
        const that = this
        if (!that.data.token) {
            wx.showToast({
                title: "操作太快了，请稍后重试",
                icon: 'none',
                duration: 2000
            })
            app.getToken()
            return;
        }
        wx.request({
            url: app.globalData.baseurl + 'park/getParkByToken',
            method: "POST",
            data: {
                token: that.data.token,
                current: current,
                state: that.data.state,
            },
            success(res) {
                if (res.data.code === 200) {
                    const dataList = res.data.data
                    if (dataList.length < 10) {
                        that.setData({isNoMoreData: true})
                    }
                    that.addOrderList(dataList)
                    that.addCurrentPage()
                }
            }
        })
    },
    /**
     * 点击触发bindChange
     */
    switchNav(e) {
        let isSelect = e.target.dataset.current;
        this.setData({
            currentTab: isSelect,
            state: isSelect + 1,
        })
    },
    /**
     * 切换swiper
     */
    bindChange() {
        const that = this
        that.setData({
            isNoMoreData: false,
            orderList: []
        })
        if (that.data.currentTab === 1) {
            if (that.data.currentPage1 === 1) {
                that.getData(that.data.currentPage1)
            }
        } else if (that.data.currentTab === 2) {
            if (that.data.currentPage2 === 1) {
                that.getData(that.data.currentPage2)
            }
        } else {
            if (that.data.currentPage0 === 1) {
                that.getData(that.data.currentPage0)
            }
        }
        that.switchOrderList()
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    scrollToLower: function () {
        if (!this.data.isNoMoreData && this.data.orderList.length > 0) {
            this.getDataByCurrentTab()
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     * 获取设备高度
     */
    onReady: function () {
        let that = this;
        wx.getSystemInfo({
            success: function (res) {
                that.setData({
                    winHeight: res.windowHeight
                })
            },
        })
    },
    /**
     * 根据currentTab获取数据
     */
    getDataByCurrentTab: function () {
        const that = this
        if (that.data.currentTab === 1) {
            that.getData(that.data.currentPage1)
        } else if (that.data.currentTab === 2) {
            that.getData(that.data.currentPage2)
        } else {
            that.getData(that.data.currentPage0)
        }
    },
    /**
     * 初始化currentTab
     */
    initializationCurrentTab: function () {
        const that = this
        if (that.data.currentTab === 1) {
            that.setData({currentPage1: 1})
        } else if (that.data.currentTab === 2) {
            that.setData({currentPage2: 1})
        } else {
            that.setData({currentPage0: 1})
        }
    },
    /**
     * 初始化OrderList数据
     */
    initializationOrderList: function () {
        const that = this
        if (that.data.currentTab === 1) {
            that.setData({orderList1: []})
        } else if (that.data.currentTab === 2) {
            that.setData({orderList2: []})
        } else {
            that.setData({orderList0: []})
        }
    },
    /**
     * currentPage递增
     */
    addCurrentPage: function () {
        const that = this
        if (that.data.currentTab === 1) {
            that.setData({currentPage1: that.data.currentPage1 + 1})
        } else if (that.data.currentTab === 2) {
            that.setData({currentPage2: that.data.currentPage2 + 1})
        } else {
            that.setData({currentPage0: that.data.currentPage0 + 1})
        }
    },

    /**
     * 添加OrderList数据
     */
    addOrderList: function (data) {
        const that = this
        if (that.data.currentTab === 1) {
            that.setData({orderList1: that.data.orderList1.concat(data)})
        } else if (that.data.currentTab === 2) {
            that.setData({orderList2: that.data.orderList2.concat(data)})
        } else {
            that.setData({orderList0: that.data.orderList0.concat(data)})
        }
        that.switchOrderList()
    },
    /**
     * 切换OrderList
     */
    switchOrderList: function () {
        const that = this
        let res;
        if (that.data.currentTab === 1) {
            res = that.data.orderList1
        } else if (that.data.currentTab === 2) {
            res = that.data.orderList2
        } else {
            res = that.data.orderList0
        }
        that.setData({orderList: res})
    },
    /**
     * 结束停车
     */
    endParkingLotButton: function () {
        const that = this
        wx.showModal({
            title: '提示',
            content: '确定结束停车？',
            showCancel: true,
            confirmText: '确认',
            cancelText: '取消',
            success: (res) => {
                if (res.confirm) {
                    wx.request({
                        url: app.globalData.baseurl + "sit-info/endParkingLot",
                        data: {token: that.data.token},
                        success: function (res) {
                            if (res.data.code === 200) {
                                const park = res.data.data
                                wx.showModal({
                                    title: '提示',
                                    content: '费用为：' + park.fee,
                                    showCancel: true,
                                    confirmText: '支付',
                                    cancelText: '取消',
                                    success: (res) => {
                                        if (res.confirm) {
                                            that.payModel(park.parkId)
                                            // wx.navigateTo({url: '../pay/pay?id='+park.parkId+'&fee='+park.fee})
                                        }
                                    }
                                })
                            } else {
                                wx.showToast({
                                    title: res.data.msg,
                                    icon: 'none',
                                    duration: 2000
                                })
                            }
                            that.fresherRefresh()
                        },
                        method: "POST"
                    })
                }
            }
        })
    },
    /**
     * 订单支付方法
     */
    parkingLotPayButton: function (e) {
        // wx.navigateTo({url: '../pay/pay?id='+e.target.dataset.parkid+'&fee='+e.target.dataset.fee})
        this.payModel(e.target.dataset.parkid)
    },
    payModel: function (e) {
        const that = this
        const userInfo = app.globalData.userInfo
        if (userInfo.password === 'yes') {
            that.setData({existencePassword: true})
        }
        that.setData({
            showModel: true,
            password: '',
            confirm_password: '',
            currentParkId: e,
        })
    },
    /**
     * 取消支付
     */
    cancelPay: function () {
        this.setData({showModel: false})
    },
    /**
     * 确认支付
     */
    confirmPay: function () {
        const that = this
        const userInfo = app.globalData.userInfo
        if (!that.data.currentParkId) {
            wx.showToast({
                title: '系统出错，请稍后重试',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        if (that.data.password.length === 0) {
            wx.showToast({
                title: '请输入密码',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        if (userInfo.password === 'no') {
            if (that.data.password !== that.data.confirm_password) {
                wx.showToast({
                    title: '两次密码不相等',
                    icon: 'none',
                    duration: 2000
                })
                return;
            }
        }
        this.setData({showModel: false})
        wx.request({
            url: app.globalData.baseurl + "user/pay",
            method: "POST",
            data: {
                token: that.data.token,
                password: that.data.password,
                parkId: that.data.currentParkId,
            },
            success: function (res) {
                if (res.data.code === 200) {
                    that.fresherRefresh()
                }
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                    duration: 2000
                })
            }
        })
    },
    /**
     * 获取输入密码
     */
    getPassword: function (e) {
        this.setData({password: e.detail.value.trim()})
    },
    /**
     * 获取输入确认密码
     */
    getConfirmPassword: function (e) {
        this.setData({confirm_password: e.detail.value.trim()})
    },
    /**
     * 订单已完成提示
     */
    parkingLotCompleteButton: function () {
        wx.showToast({
            title: "订单已完成",
            icon: 'none',
            duration: 2000
        })
    },
    /**
     * 下拉触发刷新
     */
    fresherRefresh: function () {
        const that = this
        that.setData({
            isRefresherTriggered: false,
            isNoMoreData: false,
        })
        that.initializationCurrentTab()
        that.initializationOrderList()
        that.getDataByCurrentTab()
    },
})
